import {useUserAuthStore} from '@/store/modules/auth'
import {Directive} from "vue";
// 自定义按钮权限指令
export const permission: Directive = {
    mounted(el, binding) {
        const userAuthStore = useUserAuthStore()
        // 按钮上的权限字段
        const {value} = binding
        // 当前用户所拥有的权限字段
        const permissions = userAuthStore.permissions;
        if (value && value instanceof Array && value.length > 0) {
            const permissionsRoles = value;
            // 判断按钮权限是否存在用户的权限里面
            const hasPermission = permissions.some((role) => {
                return permissionsRoles.includes(role)
            })
            if (!hasPermission) {
                el.parentNode && el.parentNode.removeChild(el)
            }
        } else {
            throw new Error("按钮权限的传递方式v-permission=['sys:role:add']")
        }

    }
}
